package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;

import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;

public class ExcelInMaterValueForWeb extends Thread {

	private final int i;
	List<MaterVo> materVoList;

	private SupplierOrderDao supplierOrderDao;

	private IMaterDao materDaoImpl;

	private ISelectFoodDao selectFoodDaoImpl;

	public ExcelInMaterValueForWeb(int i, List<MaterVo> materVoList, SupplierOrderDao supplierOrderDao, IMaterDao materDaoImpl,
			ISelectFoodDao selectFoodDaoImpl) {
		this.i = i;
		this.materVoList = materVoList;
		this.supplierOrderDao = supplierOrderDao;
		this.materDaoImpl = materDaoImpl;
		this.selectFoodDaoImpl = selectFoodDaoImpl;

	}

	@Override
	public void run() {
		try {
			MaterVo materVo = materVoList.get(i);
			int materId = materVo.getMaterId();
			int materLevel = materVo.getMaterLevel();
			int supId = materVo.getSupId();
			String materDay = materVo.getDate();
			BigDecimal materValueUndressed = materVo.getMaterValue();
			BigDecimal materValueCostUnit = materVo.getMaterValueUndressed();
			BigDecimal materWeight = new BigDecimal(materVo.getMaterWeight());
			String userId = String.valueOf(materVo.getCustId());
			int ordId = materVo.getOrdId();
			// 取出现有系统中的材料

			// 校验用户是否订单日 订购此种材料
			List<OrderSupplierPo> orderSupplierPolist = supplierOrderDao.isExistOrderSup(userId, String.valueOf(supId), materDay, materId);
			if (orderSupplierPolist != null && orderSupplierPolist.size() > 0) {

				/*
				 * MaterLevelPo materLevelPo = new MaterLevelPo();
				 * 
				 * // 计算净料价格 MathContext mc = new MathContext(5,
				 * RoundingMode.HALF_UP); BigDecimal materValueNet = null;
				 * 
				 * List<MaterLevelPo> materLevelPoList =
				 * materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId
				 * (materId, materLevel, supId, userId); if (materLevelPoList ==
				 * null || materLevelPoList.size() == 0) { MaterLevelPo
				 * materLevelPoOther =
				 * materDaoImpl.getMaterLevelPoByMaterId(materId).get(0);
				 * 
				 * BeanCopier copier = BeanCopier.create(MaterLevelPo.class,
				 * MaterLevelPo.class, false); copier.copy(materLevelPoOther,
				 * materLevelPo, null); materLevelPo.setMid(0);
				 * materLevelPo.setSupId(supId);
				 * materLevelPo.setCustId(Integer.valueOf(userId));
				 * 
				 * // 1替换当前材料表中 的价格 materValueNet =
				 * materValueUndressed.multiply(new BigDecimal(0.1)).divide(new
				 * BigDecimal(materLevelPo.getMaterProportion()), mc)
				 * .setScale(5, BigDecimal.ROUND_HALF_UP); // 改变po
				 * materLevelPo.setMaterValue(materValueNet);
				 * materLevelPo.setMaterValueUndressed
				 * (materValueUndressed.multiply(new BigDecimal(0.001)));
				 * 
				 * materDaoImpl.addMaterLevelPersist(materLevelPo); } else {
				 * materLevelPo = materLevelPoList.get(0); // 1替换当前材料表中 的价格
				 * materValueNet = materValueUndressed.multiply(new
				 * BigDecimal(0.1)).divide(new
				 * BigDecimal(materLevelPo.getMaterProportion()), mc)
				 * .setScale(5, BigDecimal.ROUND_HALF_UP); // 改变po
				 * materLevelPo.setMaterValue(materValueNet);
				 * materLevelPo.setMaterValueUndressed
				 * (materValueUndressed.multiply(new BigDecimal(0.001)));
				 * materDaoImpl.addMaterLevel(materLevelPo); }
				 * 
				 * // 2.插入材料备份表中 List<MaterLevelBlPo> materLevelBlPoList =
				 * materDaoImpl
				 * .getMaterLevelBlPoByMaterIdAndLevelAndSupId(materId,
				 * materLevel, supId, materDay, userId); MaterLevelBlPo
				 * materLevelBlPo = new MaterLevelBlPo(); if (materLevelBlPoList
				 * != null && materLevelBlPoList.size() > 0) { materLevelBlPo =
				 * materLevelBlPoList.get(0); BeanCopier copier =
				 * BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class,
				 * false); copier.copy(materLevelPo, materLevelBlPo, null);
				 * materLevelBlPo.setMaterDay(materDay);
				 * materLevelBlPo.setMaterValue(materValueNet);
				 * materLevelBlPo.setMaterValueUndressed
				 * (materValueUndressed.multiply(new BigDecimal(0.001)));
				 * materDaoImpl.updateMaterLevelBl(materLevelBlPo); } else {
				 * BeanCopier copier = BeanCopier.create(MaterLevelPo.class,
				 * MaterLevelBlPo.class, false); copier.copy(materLevelPo,
				 * materLevelBlPo, null); materLevelBlPo.setMaterDay(materDay);
				 * materLevelBlPo.setMaterValue(materValueNet);
				 * materLevelBlPo.setMaterValueUndressed
				 * (materValueUndressed.multiply(new BigDecimal(0.001)));
				 * materDaoImpl.addMaterLevelBl(materLevelBlPo); }
				 */

				// 改变所提交日期的实际价格 并生成总价
				List<OrderSupplierMaterRealPo> tOrderSupMaterRealPoList = supplierOrderDao.getOrderSupMaterRealListBySupIdOrdDateMaterIdAndCustId(
						supId, materDay, materId, userId);

				if (tOrderSupMaterRealPoList != null && tOrderSupMaterRealPoList.size() > 0) {
					for (OrderSupplierMaterRealPo orderSupplierMaterRealPo : tOrderSupMaterRealPoList) {

						orderSupplierMaterRealPo.setOrdMaterWeight(materWeight);
						orderSupplierMaterRealPo.setOrdMaterValueUnit(materValueUndressed);
						orderSupplierMaterRealPo.setOrdMaterValue(materValueUndressed.multiply(materWeight));
						BigDecimal CostUnit = new BigDecimal(0);
						orderSupplierMaterRealPo.setOrdMaterValueCostUnit(materValueCostUnit.toString());
						if (orderSupplierMaterRealPo.getOrdMaterValueCostUnit() != null) {
							CostUnit = new BigDecimal(orderSupplierMaterRealPo.getOrdMaterValueCostUnit());
						}
						orderSupplierMaterRealPo.setOrdMaterValueCostUnit(CostUnit.toString());
						orderSupplierMaterRealPo.setOrdMaterValueCost((CostUnit.multiply(materWeight)).toString());
						if (ordId == 0 || orderSupplierMaterRealPo.getOrdId() == ordId) {
							selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);
						} else if (ordId != orderSupplierMaterRealPo.getOrdId()) {
							continue;
						}

					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println("子线程" + i + "结束");
		}

	}
}